home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9110.ZIP / STRING.ZIP / STRIINS3.CPP < prev    next >
C/C++ Source or Header  |  1991-07-30  |  609b  |  29 lines

  1. #include <string.h>
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <string.hpp>
  5.  
  6. String &String::insert(int pos, char c)
  7. {
  8.     if (pos > length()) { // pos == length() is concatenate
  9.         errno = ERANGE;
  10.         return *this;
  11.     }
  12.     int n = length()+1;
  13.     srep *p = new(n) srep(n);
  14.     if (!p) {
  15.         errno = ENOMEM;
  16.         return *this;
  17.     }
  18.     memmove(p->body,body(),pos);
  19.     *(p->body+pos) = c;
  20.     memmove(p->body+pos+1, body()+pos, length()-pos);
  21.     if (rp) {
  22.         if (--rp->refs < 1)
  23.         delete rp;
  24.     }
  25.     rp = p;
  26.     rp->refs++;
  27.     return *this;
  28. }
  29.